taxi = taxi || {};
// ----------------------------

taxi.GoogleMapsViewer = function(mapLayer,utils) {
	this.utils = utils;
    
	if (mapLayer == undefined)
		return;
	this.mapLayer = mapLayer;
	this._map = new google.maps.Map(mapLayer[0], {
		zoom : 3,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		center : this.utils.toLatLng([30, -20]),
		mapTypeControl: true,
	    mapTypeControlOptions: {
	        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
	        position: google.maps.ControlPosition.BOTTOM_CENTER
	    },
	    navigationControl: true,
	    navigationControlOptions: {
	        style: google.maps.NavigationControlStyle.ZOOM_PAN,
	        position: google.maps.ControlPosition.TOP_RIGHT
	    },
	    scaleControl: true,
	    scaleControlOptions: {
	        position: google.maps.ControlPosition.RIGHT_BOTTOM
	    }		
	});
	// Initialize directions service => difficult to decouple from map provider
	this._directionsService = new google.maps.DirectionsService();
    this._routeDisplay = new google.maps.DirectionsRenderer({        
        markerOptions: {visible:false}
    });	
	
	this.fromMarker = undefined;
	this.toMarker = undefined;	
}

taxi.GoogleMapsViewer.prototype.centerTo = function(addr){	
    this._map.setOptions({
        zoom: 14,
        center: this.utils.toLatLng(addr.getLatLon())       
    });	
}

taxi.GoogleMapsViewer.prototype.setBounds = function(fromAddr,toAddr){	
	var boundaries = this.utils.toBounds([fromAddr.getLatLon(),toAddr.getLatLon()]); 

	this._map.panToBounds(boundaries);
    this._map.fitBounds(boundaries);
}

taxi.GoogleMapsViewer.prototype.drawFromLocation = function(address){
	if(this.fromMarker == undefined){
		this.fromMarker = new google.maps.Marker({
			icon: "http://live.sporza.be/html/sport/images/lmc/flag-start.png",
	        map: this._map,
	        position: this.utils.toLatLng(address.getLatLon())
	    });
	} else {
		this.fromMarker.setOptions( {
			position :  this.utils.toLatLng(address.getLatLon())
		});		
	}
}
taxi.GoogleMapsViewer.prototype.drawToLocation = function(address){
	if(this.toMarker == undefined){
		this.toMarker = new google.maps.Marker({
			icon: "http://live.sporza.be/html/sport/images/lmc/flag-finish.png",
	        map: this._map,
	        position: this.utils.toLatLng(address.getLatLon())
	    });
	} else {
		this.toMarker.setOptions( {
			map: this._map,
			position :  this.utils.toLatLng(address.getLatLon())
		});				
	}
}
taxi.GoogleMapsViewer.prototype.clearRoute = function(){
	this._routeDisplay.setMap(null);	
}

taxi.GoogleMapsViewer.prototype.resolveRoute = function(origin,destination,handler){
	var self = this;
    this._directionsService.route({
        origin: this.utils.toLatLng(origin.getLatLon()),
        destination: this.utils.toLatLng(destination.getLatLon()),
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
        unitSystem: google.maps.DirectionsUnitSystem.METRIC
    },
    function(result, status) {    	
        if (status == google.maps.DirectionsStatus.OK) {
            if (result.routes.length > 0) {
                // Render the route
                self._routeDisplay.setDirections(result);
                self._routeDisplay.setMap(self._map);
                // Describe the route
                var route = result.routes[0];
                var distanceText = route.legs[0].distance.text;
                var distanceValue = route.legs[0].distance.value;
                var durationText = route.legs[0].duration.text;
                var durationValue = route.legs[0].duration.value;
                var origin = new taxi.Address(
        				{	lat: route.legs[0].start_location.lat(), 
        					lon: route.legs[0].start_location.lng(), 
        					addressString: route.legs[0].start_address});
                var destination = new taxi.Address(
        				{	lat: route.legs[0].end_location.lat(), 
        					lon: route.legs[0].end_location.lng(), 
        					addressString: route.legs[0].end_address});
                var steps = route.legs[0].steps;
                var tmpSteps = new Array;
                for(var i=0;i<steps.length;i++){
                	tmpSteps.push(new taxi.RouteStep(i+1,steps[i].instructions,steps[i].distance.text));
                }
                var currentRoute = new taxi.Route(
                	{
                		steps: tmpSteps,
                		distance: distanceValue,
                		distanceText: distanceText,
                		duration: durationValue,
                		durationText: durationText,
                		origin: origin,
                		destination: destination               		
                	}
                );
                handler.invoke([currentRoute]);
            }
        }
    });	
	
}

taxi.GoogleMapsViewer.prototype.clearFromLocation = function(){
	if(this.fromMarker != undefined){
		this.fromMarker.setMap(null);
		this.fromMarker = undefined;
	}
}
taxi.GoogleMapsViewer.prototype.clearToLocation = function(){
	if(this.toMarker != undefined){
		this.toMarker.setMap(null);
		this.toMarker = undefined;
	}
}